using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.Service;

namespace T.FAS.Runtime.Action.Service
{
    public class ActionClientService
    {
        private static ActionClientService instance = null;
        private static readonly object lockobj = new object();
        public static ActionClientService GetInstance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockobj)
                    {
                        if (instance == null)
                            instance = new ActionClientService();
                    }
                }
                return instance;
            }
        }
        private ActionClientService()
        { }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="actionId">ActionID</param>
        /// <param name="data">数据</param>
        /// <param name="actionType">动作类型</param>
        public void SaveData(string actionId, TRecord data, SPI.ActionType actionType = ActionType.Action, bool isMatchMetadataByCode = false)
        {
            if (string.IsNullOrEmpty(actionId))
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_SaveData);
After:
                throw new ArgumentNullException(Service.ActionClientService_SaveData);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_SaveData);
            }
            if (data == null)
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_SaveData1);
After:
                throw new ArgumentNullException(Service.ActionClientService_SaveData1);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_SaveData1);
            }
            var parameterList = new List<ActionParameter>();
            parameterList.Add(new ActionParameter()
            {
                Order = 0,
                ParameterName = "TRecord",
                ParameterValue = data
            });

            var result = ActionExecuteService.GetInstance.Execute(actionId, actionType, parameterList, isMatchMetadataByCode);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="actionId">动作标识</param>
        /// <param name="dataId">数据ID</param>
        /// <param name="actionType">动作类型</param>
        /// <returns></returns>
        public TResult GetDataById(string actionId, string dataId, SPI.ActionType actionType = ActionType.Action, bool isMatchMetadataByCode = false)
        {
            if (string.IsNullOrEmpty(actionId))
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_GetDataById);
After:
                throw new ArgumentNullException(Service.ActionClientService_GetDataById);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_GetDataById);
            }
            if (string.IsNullOrEmpty(dataId))
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_GetDataById1);
After:
                throw new ArgumentNullException(Service.CommonStringClassDonotTranslated.ActionClientService_GetDataById1);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_GetDataById1);
            }
            var parameterList = new List<ActionParameter>();
            parameterList.Add(new ActionParameter() 
            { 
                Order = 0, 
                ParameterName = "ID", 
                ParameterValue = dataId 
            });

            return ActionExecuteService.GetInstance.Execute(actionId,actionType,parameterList, isMatchMetadataByCode);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="actionId">动作标识</param>
        /// <param name="dataId">数据ID</param>
        /// <param name="actionType">动作类型</param>
        public void DeleteDataById(string actionId, string dataId, SPI.ActionType actionType = ActionType.Action, bool isMatchMetadataByCode = false)
        {
            if (string.IsNullOrEmpty(actionId))
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_DeleteDataById);
After:
                throw new ArgumentNullException(Service.CommonStringClassDonotTranslated.ActionClientService_DeleteDataById);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_DeleteDataById);
            }
            if (string.IsNullOrEmpty(dataId))
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_DeleteDataById1);
After:
                throw new ArgumentNullException(Service.CommonStringClassDonotTranslated.ActionClientService_DeleteDataById1);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_DeleteDataById1);
            }
            var parameterList = new List<ActionParameter>();
            parameterList.Add(new ActionParameter()
            {
                Order = 0,
                ParameterName = "ID",
                ParameterValue = dataId
            });

            ActionExecuteService.GetInstance.Execute(actionId,actionType, parameterList, isMatchMetadataByCode);
        }
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="actionId">动作标识</param>
        /// <param name="filter">过滤条件</param>
        /// <param name="actionType">动作类型</param>
        /// <returns></returns>
        public TResult GetDataList(string actionId, List<ActionParameter> filter, SPI.ActionType actionType = ActionType.Action, bool isMatchMetadataByCode = false)
        {
            if (string.IsNullOrEmpty(actionId))
            {

/* Unmerged change from project 'T.FAS.Runtime.Action.Service (net451)'
Before:
                throw new ArgumentNullException(CommonStringClass.ActionClientService_GetDataList);
After:
                throw new ArgumentNullException(Service.CommonStringClassDonotTranslated.ActionClientService_GetDataList);
*/
                throw new ArgumentNullException(CommonStringClassDonotTranslated.ActionClientService_GetDataList);
            }

            return ActionExecuteService.GetInstance.Execute(actionId,actionType, filter, isMatchMetadataByCode);
        }
        /// <summary>
        /// 根据ID获取Record类型的结果数据
        /// </summary>
        /// <param name="actionId">动作标识</param>
        /// <param name="dataId">数据ID</param>
        /// <param name="actionType">动作类型</param>
        /// <returns></returns>
        public TRecord GetRecordById(string actionId, string dataId, SPI.ActionType actionType = ActionType.Action, bool isMatchMetadataByCode = false)
        {
            var result = GetDataById(actionId, dataId, actionType);
            return result.GetDataContent() as TRecord;
        }
        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="cdoId">CDO标识</param>
        /// <param name="columnCode">状态列编号</param>
        /// <param name="eventKey">事件Key</param>
        /// <param name="record">Record结构数据</param>
        /// <returns></returns>
        public TResult ChangeState(string cdoId,string columnCode,string eventKey,TRecord record, bool IsMatchMetadataByCode = false)
        {
            var paramterList = new List<ActionParameter>();
            paramterList.Add(new ActionParameter()
            {
                Order =0,
                ParameterName = "CDOID",
                ParameterValue = cdoId
            });
            paramterList.Add(new ActionParameter()
            {
                Order=1,
                ParameterName = "ColumnCode",
                ParameterValue = columnCode,
            });
            paramterList.Add(new ActionParameter()
            {
                Order=2,
                ParameterName= "EventKey",
                ParameterValue= eventKey
            });
            paramterList.Add(new ActionParameter()
            {
                Order =3,
                ParameterName = "TRecord",
                ParameterValue = record
            });
            return ActionExecuteService.GetInstance.Execute(cdoId,ActionType.CDO_ChangeState,paramterList, IsMatchMetadataByCode);
        }

        /// <summary>
        /// 执行状态变更事件
        /// </summary>
        /// <param name="cdoId">CDO标识</param>
        /// <param name="columnCode">状态列编号</param>
        /// <param name="eventKey">事件Key</param>
        /// <param name="billID">数据ID</param>
        /// <returns></returns>
        public TResult FireEvent(string cdoId,string columnCode,string eventKey,string billID, bool IsMatchMetadataByCode = false)
        {
            return StateMachineRTPClient.GetInstance().FireEvent(cdoId,columnCode,eventKey,billID);
        }

        /// <summary>
        /// 执行状态变更事件
        /// </summary>
        /// <param name="cdoId">CDO标识</param>
        /// <param name="columnCode">状态列编号</param>
        /// <param name="eventKey">事件Key</param>
        /// <param name="billID">数据ID</param>
        /// <param name="source">来源，例如任务中心、审批流</param>
        /// <returns></returns>
        public TResult FireEvent(string cdoId, string columnCode, string eventKey, string billID, string source = "")
        {
            return StateMachineRTPClient.GetInstance().FireEvent(cdoId, columnCode, eventKey, billID, source);
        }
    }
}
